package com.vegetable.modules.controller.baseData;


import com.alibaba.fastjson.JSON;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.baseData.BaseDict;
import com.vegetable.modules.service.baseData.IBaseDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 基础数据 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-04
 */
@Slf4j
@RestController
@RequestMapping("/baseDict")
@AllArgsConstructor
@Api(value = "基础数据", tags = "基础数据")
public class BaseDictController extends AbstractController {

    private IBaseDictService baseDictService;
    /**
     * 所有基础数据列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "条件查询基础数据列表", notes = "条件查询基础数据列表")
    public R list(@RequestParam Map<String, Object> params){
        String dictType = (String)params.get("dictType");
        if (StringUtils.isEmpty(dictType)){
            return R.fail("基础数据类型不能为空");
        }
        log.info("条件查询基础数据列表{}", JSON.toJSONString(params));
        PageUtils page = baseDictService.queryPage(params);

        return R.data( page);
    }

    /**
     * 新增基础数据
     */
    @SysLog(value = "新增基础数据")
    @PostMapping("/save")
    @ApiOperation(value = "新增基础数据", notes = "新增基础数据")
    public R save(@RequestBody BaseDict baseDict){
        baseDict.setCreateUserId(getUserId());
        baseDictService.save(baseDict);
        return R.status(true);
    }

    /**
     * 修改基础数据
     */
    @SysLog(value = "修改基础数据")
    @PostMapping("/update")
    @ApiOperation(value = "修改基础数据", notes = "修改基础数据")
    public R update(@RequestBody BaseDict baseDict){
        baseDict.setCreateUserId(getUserId());
        baseDictService.updateById(baseDict);
        return R.status(true);
    }

    /**
     * 根据id查询基础数据
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "根据id查询基础数据", notes = "根据id查询基础数据")
    public R info(@PathVariable("id") Long id){
        BaseDict baseDict = baseDictService.getById(id);
        return R.data(baseDict);
    }


    /**
     * 删除基础数据
     */
    @SysLog(value = "删除基础数据")
    @GetMapping("/delete")
    @ApiOperation(value = "删除基础数据", notes = "删除基础数据")
    public R delete(Long id){
        baseDictService.removeById(id);
        return R.status(true);
    }

}
